package com.cwj.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cwj.dao.mapper.course_videoMapper;
import com.cwj.dao.mapper.course_video_teacherMapper;
import com.cwj.service.CourseInfo;
import common.entity.course;
import common.entity.course_video;
import common.entity.teacher;
import common.utils.courseUtils;
import common.utils.pictUtils;
import common.utils.teacherUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class courseInfoImpl implements CourseInfo {
    @Resource
    ServletContext servletContext;
    @Resource
    com.cwj.dao.mapper.courseMapper courseMapper;
    @Resource
    com.cwj.dao.mapper.teacherMapper teacherMapper;
    @Resource
    com.cwj.dao.mapper.course_teacherMapper course_teacherMapper;
    @Resource
    course_videoMapper course_videoMapper;
    @Resource
    com.cwj.dao.mapper.course_video_teacherMapper course_video_teacherMapper;
    @Override
    public course getCourseMsg(Integer id, String name) {
        return courseMapper.selectById(id);
    }

    @Override
    public Map<String,teacher> getCourseTeacherList(Integer id, String name) throws Exception {
        Map<String,teacher> map = new HashMap<>();
        List<teacher> teacherList = course_teacherMapper.getCourseTeacherList(id);

        for (teacher teacher : teacherList) {
            for (String pictType : pictUtils.pictTypes) {
                File file = new File(servletContext.getRealPath(teacherUtils.parentFile + teacherUtils.PortraitsPict + teacherUtils.setCoursePictName(teacher, pictType)));
                if(file.exists()){ map.put(teacherUtils.parentFile+teacherUtils.PortraitsPict+file.getName(),teacher);break; }
            }
        }
        return map;
    }
    @Override
    public List<course_video> getCourse_video(Integer id, String name, Integer count) {
        return course_videoMapper.selectPage(new Page<>(1, count), new QueryWrapper<course_video>().eq("course_id",id)).getRecords();
    }

    @Override
    public List<teacher> getCourse_videoTeacher(Integer course_id, String id) {
        List<teacher> teacherList = course_video_teacherMapper.getCourse_videoTeacher(course_id, id);
        return teacherList;
    }

    @Override
    public Integer FindPageCount(Integer course_id, String course_name, Integer size) {
        Integer selectCount = course_videoMapper.selectCount(new QueryWrapper<course_video>().eq("course_id", course_id));
        return selectCount/size+(selectCount % size!=0? 1 : 0);
    }

    @Override
    public List<course_video> getCourse_videoByPage(Integer course_id, Integer page, Integer size) {
        Page<course_video> course_videoPage = new Page<>(page, size);
        List<course_video> videos = course_videoMapper.selectPage(course_videoPage, new QueryWrapper<course_video>().eq("course_id", course_id)).getRecords();
        return videos;
    }

    @Override
    public String findCourse_videoPict(Integer course_id, String id) throws Exception {
        course_video course_video = new course_video();course_video.setCourse_id(course_id);course_video.setId(id);
        String realPath = servletContext.getRealPath(courseUtils.parentFile + courseUtils.Course_video_cover);

        for (String pictType : pictUtils.pictTypes) {
            File file = new File(realPath + courseUtils.setCourse_video_coverName(course_video, pictType));
//            System.out.println(file.getAbsolutePath());
            if(file.exists()){ return courseUtils.parentFile+courseUtils.Course_video_cover+courseUtils.setCourse_video_coverName(course_video,pictType);}
        }
        return null;
    }

    @Override
    public int[] getMaxGrade(Integer course_id) {

        int[] grades=new int[courseUtils.MaxGrade+1];grades[0]=1;
        QueryWrapper<course_video> wrapper1 = new QueryWrapper<course_video>().eq("course_id", course_id);
         StringBuilder sb=new StringBuilder(courseUtils.MaxGradeLike);
        for(int grade=courseUtils.MaxGrade;grade>=1;grade--){
            grades[grade] = course_videoMapper.selectCount(wrapper1.like("id", sb.toString()));
            wrapper1.clear();wrapper1.eq("course_id",course_id);
           sb.delete(0,2);
        }
        int[] ints = new int[courseUtils.MaxGrade + 1];
        System.arraycopy(grades,0,ints,0,grades.length);
        for(int i=courseUtils.MaxGrade;i>=1;i--){
            if(i==courseUtils.MaxGrade){continue;}
            grades[i]=ints[i]-ints[i+1];
        }
        System.out.println(Arrays.toString(grades));
        return grades;
    }
}
